package com.acme.bankapp.gui;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JTextField;
import java.awt.GridBagLayout;
import javax.swing.JLabel;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import javax.swing.JTextArea;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.net.UnknownHostException;

import javax.swing.JToggleButton;
import com.acme.bankapp.util.Utils;
import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;
import javax.swing.JFormattedTextField;

import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;
import com.acme.bankapp.domain.bank.Gender;
import com.acme.bankapp.domain.bank.AccountType;
import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;

public class AddClientFrame extends JFrame {

    private JPanel contentPane;
    private JTextField hostNameTextField;
    private JLabel lblPort;
    private JFormattedTextField portTextField;
    private JTextArea textArea;
    private JToggleButton tglbtnConnect;

    Socket requestSocket;
    ObjectOutputStream out;
    ObjectInputStream in;
    private JLabel lblClientName;
    private JTextField txtName;
    private JLabel lblGender;
    private JComboBox comboBoxGender;
    private JLabel lblAccountType;
    private JComboBox comboBoxAccountType;
    private JLabel lblAccountId;
    private JSpinner spinnerAccountId;
    private JLabel lblBalance;
    private JSpinner spinnerBalance;
    private JLabel lblOverdraft;
    private JSpinner spinnerOverdraft;
    private JButton btnSend;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
	EventQueue.invokeLater(new Runnable() {
	    public void run() {
		try {
		    AddClientFrame frame = new AddClientFrame();
		    frame.setVisible(true);
		} catch (Exception e) {
		    e.printStackTrace();
		}
	    }
	});
    }

    /**
     * Create the frame.
     */
    public AddClientFrame() {
	setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

	setBounds(100, 100, 450, 300);
	contentPane = new JPanel();
	contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
	setContentPane(contentPane);
	GridBagLayout gbl_contentPane = new GridBagLayout();
	gbl_contentPane.columnWidths = new int[] { 20, 125, 0, 0, 0, 0 };
	gbl_contentPane.rowHeights = new int[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
	gbl_contentPane.columnWeights = new double[] { 0.0, 1.0, 0.0, 1.0, 0.0,
		Double.MIN_VALUE };
	gbl_contentPane.rowWeights = new double[] { 0.0, 0.0, 0.0, 0.0, 0.0,
		0.0, 0.0, 0.0, 1.0, Double.MIN_VALUE };
	contentPane.setLayout(gbl_contentPane);

	JLabel lblHostName = new JLabel("host name:");
	GridBagConstraints gbc_lblHostName = new GridBagConstraints();
	gbc_lblHostName.fill = GridBagConstraints.HORIZONTAL;
	gbc_lblHostName.insets = new Insets(0, 0, 5, 5);
	gbc_lblHostName.gridx = 0;
	gbc_lblHostName.gridy = 0;
	contentPane.add(lblHostName, gbc_lblHostName);

	hostNameTextField = new JTextField();
	hostNameTextField.setText("localhost");
	GridBagConstraints gbc_hostNameTextField = new GridBagConstraints();
	gbc_hostNameTextField.fill = GridBagConstraints.HORIZONTAL;
	gbc_hostNameTextField.insets = new Insets(0, 0, 5, 5);
	gbc_hostNameTextField.gridx = 1;
	gbc_hostNameTextField.gridy = 0;
	contentPane.add(hostNameTextField, gbc_hostNameTextField);
	hostNameTextField.setColumns(10);

	lblPort = new JLabel("port:");
	GridBagConstraints gbc_lblPort = new GridBagConstraints();
	gbc_lblPort.fill = GridBagConstraints.HORIZONTAL;
	gbc_lblPort.insets = new Insets(0, 0, 5, 5);
	gbc_lblPort.gridx = 2;
	gbc_lblPort.gridy = 0;
	contentPane.add(lblPort, gbc_lblPort);

	portTextField = new JFormattedTextField();
	portTextField.setText("2004");
	GridBagConstraints gbc_portTextField = new GridBagConstraints();
	gbc_portTextField.insets = new Insets(0, 0, 5, 5);
	gbc_portTextField.fill = GridBagConstraints.HORIZONTAL;
	gbc_portTextField.gridx = 3;
	gbc_portTextField.gridy = 0;
	contentPane.add(portTextField, gbc_portTextField);
	portTextField.setColumns(10);

	tglbtnConnect = new JToggleButton("Connect");
	tglbtnConnect.addItemListener(new ItemListener() {
	    public void itemStateChanged(ItemEvent ev) {
		if (ev.getStateChange() == ItemEvent.SELECTED) {
		    out = null;
		    in = null;
		    int port = 0;
		    try {
			port = Integer.parseInt(portTextField.getText());
			requestSocket = new Socket(hostNameTextField.getText(),
				port);
			out = new ObjectOutputStream(requestSocket
				.getOutputStream());
			out.flush();
			in = new ObjectInputStream(requestSocket
				.getInputStream());
			textArea.append("Connected to "
				+ hostNameTextField.getText() + " in port "
				+ port + "\n");
			textArea.append("server>" + (String) in.readObject()
				+ "\n");
		    } catch (NumberFormatException e) {
			textArea.append("invalid port number\n");
		    } catch (UnknownHostException e) {
			textArea.append("Unknown host\n");
		    } catch (IOException e) {
			textArea.append(Utils.getErrorMessage(e) + "\n");
		    } catch (ClassNotFoundException e) {
			e.printStackTrace();
		    } finally {
			if (in == null) {
			    tglbtnConnect.setSelected(false);
			    return;
			}
		    }
		} else {
		    try {
			if (out != null) {
			    out.writeObject(new String("bye"));
			    textArea.append("server>"
				    + (String) in.readObject() + "\n");
			}
		    } catch (IOException e) {
			textArea.append(Utils.getErrorMessage(e) + "\n");
		    } catch (ClassNotFoundException e) {
			e.printStackTrace();
		    } finally {
			try {
			    requestSocket.close();
			} catch (IOException e) {
			    // don't care
			}
		    }
		}
	    }
	});
	GridBagConstraints gbc_tglbtnConnect = new GridBagConstraints();
	gbc_tglbtnConnect.insets = new Insets(0, 0, 5, 0);
	gbc_tglbtnConnect.gridx = 4;
	gbc_tglbtnConnect.gridy = 0;
	contentPane.add(tglbtnConnect, gbc_tglbtnConnect);

	lblClientName = new JLabel("client name:");
	GridBagConstraints gbc_lblClientName = new GridBagConstraints();
	gbc_lblClientName.anchor = GridBagConstraints.EAST;
	gbc_lblClientName.insets = new Insets(0, 0, 5, 5);
	gbc_lblClientName.gridx = 0;
	gbc_lblClientName.gridy = 1;
	contentPane.add(lblClientName, gbc_lblClientName);

	txtName = new JTextField();
	txtName.setText("Anton");
	GridBagConstraints gbc_txtName = new GridBagConstraints();
	gbc_txtName.insets = new Insets(0, 0, 5, 5);
	gbc_txtName.fill = GridBagConstraints.HORIZONTAL;
	gbc_txtName.gridx = 1;
	gbc_txtName.gridy = 1;
	contentPane.add(txtName, gbc_txtName);
	txtName.setColumns(10);

	lblGender = new JLabel("gender:");
	GridBagConstraints gbc_lblGender = new GridBagConstraints();
	gbc_lblGender.anchor = GridBagConstraints.EAST;
	gbc_lblGender.insets = new Insets(0, 0, 5, 5);
	gbc_lblGender.gridx = 2;
	gbc_lblGender.gridy = 1;
	contentPane.add(lblGender, gbc_lblGender);

	comboBoxGender = new JComboBox();
	comboBoxGender.setModel(new DefaultComboBoxModel(Gender.values()));
	comboBoxGender.setMaximumRowCount(2);
	GridBagConstraints gbc_comboBoxGender = new GridBagConstraints();
	gbc_comboBoxGender.insets = new Insets(0, 0, 5, 5);
	gbc_comboBoxGender.fill = GridBagConstraints.HORIZONTAL;
	gbc_comboBoxGender.gridx = 3;
	gbc_comboBoxGender.gridy = 1;
	contentPane.add(comboBoxGender, gbc_comboBoxGender);

	lblAccountType = new JLabel("account type:");
	GridBagConstraints gbc_lblAccountType = new GridBagConstraints();
	gbc_lblAccountType.anchor = GridBagConstraints.EAST;
	gbc_lblAccountType.insets = new Insets(0, 0, 5, 5);
	gbc_lblAccountType.gridx = 0;
	gbc_lblAccountType.gridy = 2;
	contentPane.add(lblAccountType, gbc_lblAccountType);

	comboBoxAccountType = new JComboBox();
	comboBoxAccountType.setModel(new DefaultComboBoxModel(AccountType
		.values()));
	comboBoxAccountType.setMaximumRowCount(2);
	GridBagConstraints gbc_comboBoxAccountType = new GridBagConstraints();
	gbc_comboBoxAccountType.insets = new Insets(0, 0, 5, 5);
	gbc_comboBoxAccountType.fill = GridBagConstraints.HORIZONTAL;
	gbc_comboBoxAccountType.gridx = 1;
	gbc_comboBoxAccountType.gridy = 2;
	contentPane.add(comboBoxAccountType, gbc_comboBoxAccountType);

	lblAccountId = new JLabel("account id:");
	GridBagConstraints gbc_lblAccountId = new GridBagConstraints();
	gbc_lblAccountId.insets = new Insets(0, 0, 5, 5);
	gbc_lblAccountId.gridx = 2;
	gbc_lblAccountId.gridy = 2;
	contentPane.add(lblAccountId, gbc_lblAccountId);

	spinnerAccountId = new JSpinner();
	spinnerAccountId.setModel(new SpinnerNumberModel(0, 0, 100, 1));
	GridBagConstraints gbc_spinnerAccountId = new GridBagConstraints();
	gbc_spinnerAccountId.fill = GridBagConstraints.HORIZONTAL;
	gbc_spinnerAccountId.insets = new Insets(0, 0, 5, 5);
	gbc_spinnerAccountId.gridx = 3;
	gbc_spinnerAccountId.gridy = 2;
	contentPane.add(spinnerAccountId, gbc_spinnerAccountId);

	btnSend = new JButton("Send");
	btnSend.addActionListener(new ActionListener() {
	    public void actionPerformed(ActionEvent e) {
		if (out != null) {
		    try {
			Character at = comboBoxAccountType.getSelectedIndex() == 0 ? 'c'
				: 's';
			Character g = comboBoxGender.getSelectedIndex() == 0 ? 'm'
				: 'f';
			out.writeObject(new String("add accountid="
				+ spinnerAccountId.getValue() + ";accounttype="
				+ at + ";balance=" + spinnerBalance.getValue()
				+ ";overdraft=" + spinnerOverdraft.getValue()
				+ ";name=" + txtName.getText() + ";gender=" + g
				+ ";"));
			textArea.append((String) in.readObject() + "\n");
		    } catch (IOException e1) {
			e1.printStackTrace();
		    } catch (ClassNotFoundException e1) {
			e1.printStackTrace();
		    }
		}
	    }
	});
	GridBagConstraints gbc_btnSend = new GridBagConstraints();
	gbc_btnSend.insets = new Insets(0, 0, 5, 0);
	gbc_btnSend.gridx = 4;
	gbc_btnSend.gridy = 2;
	contentPane.add(btnSend, gbc_btnSend);

	lblBalance = new JLabel("balance:");
	GridBagConstraints gbc_lblBalance = new GridBagConstraints();
	gbc_lblBalance.insets = new Insets(0, 0, 5, 5);
	gbc_lblBalance.gridx = 0;
	gbc_lblBalance.gridy = 3;
	contentPane.add(lblBalance, gbc_lblBalance);

	spinnerBalance = new JSpinner();
	spinnerBalance.setModel(new SpinnerNumberModel(new Double(0), null,
		null, new Double(1)));
	GridBagConstraints gbc_spinnerBalance = new GridBagConstraints();
	gbc_spinnerBalance.fill = GridBagConstraints.HORIZONTAL;
	gbc_spinnerBalance.insets = new Insets(0, 0, 5, 5);
	gbc_spinnerBalance.gridx = 1;
	gbc_spinnerBalance.gridy = 3;
	contentPane.add(spinnerBalance, gbc_spinnerBalance);

	lblOverdraft = new JLabel("overdraft:");
	GridBagConstraints gbc_lblOverdraft = new GridBagConstraints();
	gbc_lblOverdraft.insets = new Insets(0, 0, 5, 5);
	gbc_lblOverdraft.gridx = 2;
	gbc_lblOverdraft.gridy = 3;
	contentPane.add(lblOverdraft, gbc_lblOverdraft);

	spinnerOverdraft = new JSpinner();
	spinnerOverdraft.setModel(new SpinnerNumberModel(new Double(0),
		new Double(0), null, new Double(1)));
	GridBagConstraints gbc_spinnerOverdraft = new GridBagConstraints();
	gbc_spinnerOverdraft.fill = GridBagConstraints.HORIZONTAL;
	gbc_spinnerOverdraft.insets = new Insets(0, 0, 5, 5);
	gbc_spinnerOverdraft.gridx = 3;
	gbc_spinnerOverdraft.gridy = 3;
	contentPane.add(spinnerOverdraft, gbc_spinnerOverdraft);

	textArea = new JTextArea();
	textArea.setLineWrap(true);
	GridBagConstraints gbc_textArea = new GridBagConstraints();
	gbc_textArea.insets = new Insets(0, 0, 0, 5);
	gbc_textArea.gridheight = 3;
	gbc_textArea.gridwidth = 5;
	gbc_textArea.fill = GridBagConstraints.BOTH;
	gbc_textArea.gridx = 0;
	gbc_textArea.gridy = 6;
	contentPane.add(textArea, gbc_textArea);
    }

}
